Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidd: true,
    linkTel: "",
    buthidden: false, //按钮的可用和不可用的控制
    VerifyCode: "获取验证码",
    width: 0,
    height: 0,
    hidden: true,
    hidden1: true,
    code: "",
    encry: "",
    iv: "",
    rawData: "",
    signature: "",
    phoneNumber: "",
    pay: "",
    code: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this
    var _this = this;
    wx.getSystemInfo({
      success: function success(res) {
        _this.setData({
          width: res.windowWidth,
          height: res.windowHeight
        });
      }
    });
    wx.login({
      success: function (res) {
        if (res.code) {
          console.log(res)
          //发起网络请求
          wx.request({
            url: 'https://weixin.dingzhuobo.com/wxa/user/login',
            data: {
              code: res.code
            },
            success: function (res) {
              console.log(res)
              var data = res.data;
              if (data.errcode !== 500) {
                wx.setStorageSync("sessionId", data.sessionId);

                // resolve(res);
              } else {
                reject(data);
              }
            }
          })
          that.setData({
            code: res.code,
            hidden: false
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  gotoInfo: function () {
    wx.navigateTo({
      url: '../member/info/info',
    })
  },
  //音频
  gotoaudio:function(){
    wx.navigateTo({
      url: '../audio/audio',
    })
  },

upload:function(){
  wx.navigateTo({
    url: '../file/upload/upload',
  })
},
gotoBack:function(){
wx.navigateTo({
  url: '../1/1',
})
  },
  gotoForm: function (e) {
    wx.navigateTo({
      url: '../forms/forms',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  gotoPay: function () {
    wx.navigateTo({
      url: '../member/pay/pay',
    })
  },
  gotoCSD: function () {
    wx.navigateTo({
      url: '../customservice/customservice',
    })
  },
  gotoOrderlist: function (e) {
    wx.navigateTo({
      url: '../medialist/order/order',
    })
  },
  gotoCollectlist:function(){
wx.navigateTo({
  url: '../medialist/collect/collect',
})
  },


  // 小程序官方组件
  gotoofficial:function(){
    wx.navigateTo({
      url: '../component/index',
    })
  },

  //slide轮播
  gotoslide:function(){
    wx.navigateTo({
      url: '../slide/slide',
    })
  },

  location:function(){
    wx.navigateTo({
      url: '../navigation/navigation',
    })
  },

    /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getInfo: function (e) {
    wx.request({
      url: 'https://weixin.dingzhuobo.com/member/infoBySessionid',
      header: {
        'wxa-sessionid': wx.getStorageSync("sessionId")
      },
      success: function (res) {
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  blurTel: function (e) {
    var linkTel = e.detail.value.replace(/\s/g, "");
    this.setData({
      linkTel: linkTel
    })
  },
  blurCode: function (e) {
    console.log(e)
    var code = e.detail.value;
    this.setData({
      code: code
    })
  },
  order: function () {
    wx.request({
      url: "https://weixin.dingzhuobo.com/order",
      // method: 'POST',
      header: {
        'wxa-sessionid': wx.getStorageSync("sessionId")
      },
      data: {
        "memberId": "2",
        "roomId": "105",
        "orderTime": "2018-02-15 06:30",
        "remark": "提前把水烧开！"
      },
      success: function (res) {
        console.log(res)
      }
    })
  },
  gotouserinfo: function (e) {
    console.log(e)
    var code = e.currentTarget.dataset.code
    var mp = e.currentTarget.dataset.mp
    // console.log(code)
    wx.request({
      url: "https://weixin.dingzhuobo.com/sms/checkCode",
      // method: 'POST',
      header: {
        'wxa-sessionid': wx.getStorageSync("sessionId")
      },
      data: {
        "mp": mp,
        "code": code,
      },
      success: function (res) {
        console.log(res)
      }
    })
  },
  userInfo: function (e) {
    console.log(e)
    wx.request({
      url: "https://weixin.dingzhuobo.com/member",
      // method: 'POST',
      header: {
        'wxa-sessionid': wx.getStorageSync("sessionId")
      },
      data: {
        "mp": "18332969975",
        "name": "刘宁",
        "gender": "1"
      },
      success: function (res) {
        console.log(res)
      }
    })
  },
  setVerify: function (e) {//发送验证码
    if (this.data.linkTel.trim() != "") {
      var linkTel = this.data.linkTel;
      console.log(linkTel)
      var _Url = "https://weixin.dingzhuobo.com/sms";
      var total_micro_second = 60 * 1000;
      //验证码倒计时
      this.setData({
        buthidden: true
      })

      count_down(this, total_micro_second);
      wx.request({
        url: _Url,
        // method: 'POST',
        header: {
          'wxa-sessionid': wx.getStorageSync("sessionId")
        },
        data: {
          "mp": linkTel
        },
        success: function (res) {
          console.log(res)
          if (res.data.code == "1") {
            wx.showModal({
              title: '提示',
              content: '发送验证码成功！',
              showCancel: false
            })
          }
        },
        fail: function (res) {
        }
      });
    }
    else {
      var that = this
      that.setData({
        hidd: false
      })
      setTimeout(function () {
        that.setData({
          hidd: true
        })
      }, 2000)
    }
  }
});
function count_down(that, total_micro_second) {
  if (total_micro_second <= 0) {
    that.setData({
      VerifyCode: "重新发送",
      buthidden: false
    });
    // timeout则跳出递归
    return;
  }
  // 渲染倒计时时钟
  that.setData({
    VerifyCode: date_format(total_micro_second) + " 秒"
  });
  setTimeout(function () {
    // 放在最后--
    total_micro_second -= 10;
    count_down(that, total_micro_second);
  }, 10)
}
// 时间格式化输出，如03:25:19 86。每10ms都会调用一次
function date_format(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600);
  // 分钟位
  var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
  // 秒位
  var sec = fill_zero_prefix((second - hr * 3600 - min * 60));// equal to => var sec = second % 60;
  // 毫秒位，保留2位
  var micro_sec = fill_zero_prefix(Math.floor((micro_second % 1000) / 10));
  return sec;
}
// 位数不足补零
function fill_zero_prefix(num) {
  return num < 10 ? "0" + num : num
}